﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTrader.Chart
{
    public partial class CmeMstBuySellChartFrm : System.Windows.Forms.Form
    {
        public CmeMstBuySellChartFrm()
        {
            InitializeComponent();
        }

        // Cybos Plus Data Set Member
        private ETDataSet m_CPDataSet = null;
        public EasyTrader.ETDataSet CPDataSet
        {
            get { return m_CPDataSet; }
            set { m_CPDataSet = value; }
        }
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private void InitTimer()
        {
            timer.Interval = 1000;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }

        // 여기서 차트를 갱신해 준다.
        private void OnTimerTick(object sender, EventArgs e)
        {
            // 차트에 데이터를 대응시켜 준다.
            SetChartData();
            ShowCurValue();
        }

        private void ShowCurValue()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null || GlobalVar.CmeMstDataDownloaded == false)
                return;

            CmeMstTable dataTable = m_CPDataSet.GetCmeMstTable();
            int rowCount = dataTable.GetRowCount();
            DataRow row = dataTable.Rows[rowCount - 1];
            double sellVal = Convert.ToDouble(row["매도총호가건수"]);
            double buyVal = Convert.ToDouble(row["매수총호가건수"]);
            string curTime = row["시각"].ToString();
            if (curTime != "0")
            {
                string minText = curTime.Substring(curTime.Length - 2, 2);
                string hourText = curTime.Substring(0, curTime.Length - 2);

                txtCurTime.Text = "현재시간 " + hourText + " : " + minText;
                txtCurPrice.Text = "현재가격 : 매수건수 " + buyVal.ToString("F2") + "매도건수 " + sellVal.ToString("F2");
            }
        }

        private void SetChartData()
        {
            // 데이터 셋 객체가 없으면 아무일도 하지 않는다.
            if (m_CPDataSet == null || GlobalVar.CmeMstDataDownloaded == false)
                return;

            CmeMstBuySellChartCtrl.Series["매도호가"].Points.Clear();
            CmeMstBuySellChartCtrl.Series["매수호가"].Points.Clear();
            CmeMstTable dataTable = m_CPDataSet.GetCmeMstTable();
            double maxValue = -10000.0;
            double minValue = 10000.0;
            int rowCount = dataTable.GetRowCount();
            int minIndex = 0;
            int maxIndex = 0;
            double buyValue = 0;
            double sellValue = 0;
            for (int i = 0; i < rowCount; i++)
            {
                DataRow row = dataTable.Rows[i];
                try
                {
                    // 값이 없을 경우는 넘어 간다.
                    if (row["매수총호가건수"] == DBNull.Value)
                        continue;

                    buyValue = Convert.ToDouble(row["매수총호가건수"]);
                    sellValue = Convert.ToDouble(row["매도총호가건수"]);
                    CmeMstBuySellChartCtrl.Series["매수호가"].Points.AddXY(i, buyValue);
                    CmeMstBuySellChartCtrl.Series["매도호가"].Points.AddXY(i, sellValue);
                    CmeMstBuySellChartCtrl.Series["매수호가"].Points[i].XValue = i;
                    //CmeMstBuySellChartCtrl.Series["매수호가"].Points[i].AxisLabel = row["시각"].ToString();
                    int hour = Convert.ToInt32(row["시각"]);
                    if (hour != 0)
                        CmeMstBuySellChartCtrl.Series["매수호가"].Points[i].AxisLabel = row["시각"].ToString();
                    else
                        CmeMstBuySellChartCtrl.Series["매수호가"].Points[i].AxisLabel = GlobalVar.NightIndexToTime(i).ToString();
                    

                    if (buyValue != 0 && buyValue > maxValue)
                    {
                        maxValue = buyValue;
                        maxIndex = i;
                    }

                    if (sellValue != 0 && sellValue > maxValue)
                    {
                        maxValue = sellValue;
                        maxIndex = i;
                    }

                    if (sellValue != 0 && sellValue < minValue)
                    {
                        minValue = sellValue;
                        minIndex = i;
                    }

                    if (buyValue != 0 && buyValue < minValue)
                    {
                        minValue = buyValue;
                        minIndex = i;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            int valueHeight = (int)maxValue / 50;
            maxValue = (valueHeight + 1) * 50;
            //minValue = 0;

            if (maxValue >= minValue)
            {
                CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.Minimum = minValue;
                CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.Maximum = maxValue;
            }
        }

        public CmeMstBuySellChartFrm(ETDataSet a_DataSet)
        {
            InitializeComponent();
            m_CPDataSet = a_DataSet;
            InitControl();
        }

        private void InitControl()
        {
            CmeMstBuySellChartCtrl.Series["매수호가"].ToolTip = "#VALX : #VAL";
            CmeMstBuySellChartCtrl.Series["매도호가"].ToolTip = "#VALX : #VAL";
        }

        private void InitChart()
        {
            CmeMstBuySellChartCtrl.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(211)), ((System.Byte)(223)), ((System.Byte)(240)));
            CmeMstBuySellChartCtrl.BackSecondaryColor = System.Drawing.Color.White;
            CmeMstBuySellChartCtrl.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            CmeMstBuySellChartCtrl.BorderlineColor = System.Drawing.Color.FromArgb(((System.Byte)(26)), ((System.Byte)(59)), ((System.Byte)(105)));
            CmeMstBuySellChartCtrl.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            CmeMstBuySellChartCtrl.BorderlineWidth = 2;
            CmeMstBuySellChartCtrl.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;

            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.MajorGrid.Interval = 10;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.Interval = 10;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.MajorGrid.Interval = 50;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.MinorGrid.Interval = 25;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.Interval = 25;

            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.IsClustered = true;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.Perspective = 10;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.IsRightAngleAxes = false;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.WallWidth = 0;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.Inclination = 15;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Area3DStyle.Rotation = 10;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.IsLabelAutoFit = true;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.LabelStyle.IsEndLabelVisible = false;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.IsLabelAutoFit = true;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.IsStartedFromZero = false;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.MinorGrid.LineColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].AxisY.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.DashDot;

            //CmeMstBuySellChartCtrl.ChartAreas["Price"].AxisY.LabelAutoFitMaxFontSize;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].BackSecondaryColor = System.Drawing.Color.White;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].BorderColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64)));
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].Position.Auto = true;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].Position.Height = 80F;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].Position.Width = 80F;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].Position.X = 5F;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].Position.Y = 10F;
            CmeMstBuySellChartCtrl.ChartAreas["BuySell"].ShadowColor = System.Drawing.Color.Transparent;
            
            //CmeMstBuySellChartCtrl.Series["지수"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
             

            //CmeMstBuySellChartCtrl.Series[0]["PriceUpColor"] = "blue";
            //CmeMstBuySellChartCtrl.Series[0]["PriceDownColor"] = "Red";
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].AxisX.LabelStyle.IsEndLabelVisible = true;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].AxisX.IsMarginVisible = true;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].AxisY.LabelStyle.IsEndLabelVisible = true;
            //CmeMstBuySellChartCtrl.ChartAreas["Price"].AxisY.IsMarginVisible = true;
            //CmeMstBuySellChartCtrl.Series[0].Color = Color.Red;
            //DataPointCustomProperties aa = CmeMstBuySellChartCtrl.Series[0]["PriceUpColor"];
        }

        private void CmeMstBuySellChart_Load(object sender, EventArgs e)
        {
            InitChart();
            InitTimer();
            GlobalVar.CmeMstRefCount++;
        }

        private void CmeMstBuySellChart_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Stop();
            GlobalVar.CmeMstRefCount--;
        }
    }
}
